//
//  CJYCAlertView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/6/13.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "CJYCAlertView.h"
#import "SYRequestAPI+TongYong.h"
#import "NoticeStudentModel.h"

@implementation CJYCAlertView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andArray:(NSArray *)titleArray andMessageID:(NSString *)msgID andModel:(CJPMYiChangModel *)model{
    if([super initWithFrame:frame]){
        self.dataArray = titleArray;
        self.msgID = msgID;
        self.model = model;
        self.frame = CGRectMake(0, 0, 500, 500);
        [self initSubView];
    }
    return self;
}
- (UITableView *)tableView{
    if (!_tableView) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 50, self.frame.size.width, self.frame.size.height-50) style:UITableViewStylePlain];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.delegate = self;
        tableView.dataSource = self;
        [tableView registerClass:[CJYCAlertCell class] forCellReuseIdentifier:@"CJYCAlertCell"];
        _tableView = tableView;
    }
    return _tableView;
}
- (void)initSubView{
    self.myDataArray = [NSMutableArray new];
    self.layer.cornerRadius = 5;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = 1;
    self.layer.borderColor = LINCOLOR.CGColor;
    CGFloat W = 1.0/_dataArray.count;
    UILabel *contLab = [UILabel new];
    for (int i = 0; i<_dataArray.count; i++) {
        if (i == 0) {
            UILabel *label = [[UILabel alloc]init];
            label.textAlignment = 1;
            label.textColor = TITLECOLOR;
            label.text = _dataArray[i];
            label.font = [UIFont systemFontOfSize:15];
            [self addSubview:label];
            contLab = label;
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@0);
                make.top.equalTo(@0);
                make.width.equalTo(self).multipliedBy(W);
                make.height.mas_equalTo(50);
            }];
        } else {
            UILabel *label = [[UILabel alloc]init];
            label.textAlignment = 1;
            label.textColor = TITLECOLOR;
            label.font = [UIFont systemFontOfSize:15];
            [self addSubview:label];
            label.text = _dataArray[i];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(contLab.mas_right);
                make.top.equalTo(@0);
                make.width.equalTo(self).multipliedBy(W);
                make.height.mas_equalTo(50);
            }];
            contLab = label;
        }
    }
    [self addSubview:self.tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@50);
        make.bottom.equalTo(@0);
    }];
    [self getDataOfKaoShiUpOrDownRequest];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _myDataArray.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CJYCAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CJYCAlertCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[CJYCAlertCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CJYCAlertCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NoticeStudentModel *mo = _myDataArray[indexPath.row];
    [cell reloadView:@[mo.Title,mo.ExamDate,mo.Score,mo.ClassOrder]];
    return cell;
}
#pragma mark - 得到考试上升下降详情数据
- (void)getDataOfKaoShiUpOrDownRequest{
    NSDictionary * parama = @{@"msgId":_msgID,
                              @"studentId":_model.StudentId};
     [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getNoticeReceiveServiceGetStudentOrderChangeDetailInfo:parama success:^(id  _Nonnull responseObject) {
//        NSLog(@"上升下降详情数据成功 %@",responseObject);
        NSArray *array = [responseObject objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            NoticeStudentModel *mod = [NoticeStudentModel yy_modelWithJSON:dic];
            [self->_myDataArray addObject:mod];
        }
        [self->_tableView reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
//        NSLog(@"上升下降详情数据失败 %@",error);
        [MBProgressHUD mj_hideHUDForView:self];
    }];
}
- (void)drawRect:(CGRect)rect{
    UIBezierPath *path = [[UIBezierPath alloc]init];
    [path moveToPoint:CGPointMake(0,50)];
    [path addLineToPoint:CGPointMake(500, 50)];
    [LINCOLOR set];
    [path stroke];
    CGFloat W = 500.0/4.0;
    for (int i = 1; i<4; i++) {
        UIBezierPath *pathNext =[[UIBezierPath alloc]init];
        [pathNext moveToPoint:CGPointMake(W*i,0)];
        [pathNext addLineToPoint:CGPointMake(W*i, 50)];
        [LINCOLOR set];
        [pathNext stroke];
    }
}
@end
